package com.yinhai.message.manage.sysapp.controller;

import java.util.List;

import com.yinhai.core.app.ta3.web.controller.BaseController;
import com.yinhai.core.common.api.base.IPage;
import com.yinhai.core.common.ta3.dto.TaParamDto;
import com.yinhai.message.core.po.MessageAppManagePo;
import com.yinhai.message.manage.sysapp.bpo.ISysAppBpo;
import com.yinhai.message.manage.sysapp.vo.SysAppVo;
import com.yinhai.modules.codetable.api.util.CodeTableUtil;
import com.yinhai.modules.org.ta3.domain.password.Md5PasswordEncrypterWithUserId;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

/**
 * 接入系统管理
 *
 * @author yanglq 2017.3.24
 */
@RequestMapping("sysAppManage")
@Controller
public class SysAppManageController extends BaseController {

    @Resource(name = "sysAppBpo")
    private ISysAppBpo sysAppBpo;

    @RequestMapping("sysAppManageController.do")
    public String execute() {
        return "manage/sysapp/sysAppManage";
    }

    @RequestMapping("getList.do")
    public String getList() {
        TaParamDto paramDto = getTaDto();
        int start = paramDto.getStart("grid1");
        int limit = paramDto.getLimit("grid1");
        IPage<SysAppVo> list = sysAppBpo.getSysAppList(paramDto, start, limit, "grid1");
        setList("grid1", list);
        return JSON;
    }

    @RequestMapping("toNew.do")
    public String toNew(ModelMap modelMap) {
        modelMap.addAttribute("codeList", CodeTableUtil.getCodeList("SENDMODE", null));
        return "manage/sysapp/sysAppToAdd";
    }

    @RequestMapping("toEdit.do")
    public String toEdit(ModelMap modelMap, @RequestParam String id) {
        modelMap.addAttribute("id", id);
        modelMap.addAttribute("codeList", CodeTableUtil.getCodeList("SENDMODE", null));
        modelMap.addAttribute("sysAppVo", sysAppBpo.getSysAppById(id));
        return "manage/sysapp/sysAppToEdit";
    }

    @RequestMapping("save.do")
    public String saveInfo() {
        TaParamDto taDto = getTaDto();
        String systemid = taDto.getAsString("appid");
        Long count = sysAppBpo.getCountBySysId(systemid, null);
        if (count == 0) {
            MessageAppManagePo po = buildPo(taDto, sysAppBpo.getSequeue());
            sysAppBpo.addSysApp(po);
            setData("ticket", po.getTicket());
        } else {
            setSuccess(false);
            setMessage("系统ID已经存在");
        }
        return JSON;
    }

    private MessageAppManagePo buildPo(TaParamDto taDto, String id) {
        String systemid = taDto.getAsString("appid");
        String systemname = taDto.getAsString("systemname");
        String sendertypeorder = taDto.getAsString("sendertypeorder");
        String senderTypeOrderDesc = taDto.getAsString("sendertypeorderdesc");
        String encodeTicket = Md5PasswordEncrypterWithUserId.getInstance().encodePassword(systemid, senderTypeOrderDesc);
        return new MessageAppManagePo(id, systemid, systemname, taDto.getUser().getUserId(), sendertypeorder, senderTypeOrderDesc, encodeTicket);
    }

    @RequestMapping("modify.do")
    public String modify() {
        TaParamDto taDto = getTaDto();
        String id = taDto.getAsString("id");
        String systemid = taDto.getAsString("appid");
        Long count = sysAppBpo.getCountBySysId(systemid, id);
        if (count == 0) {
            MessageAppManagePo po = buildPo(taDto, id);
            sysAppBpo.modifySysApp(po);
            setData("ticket", po.getTicket());
        } else {
            setSuccess(false);
            setMessage("系统ID已经存在");
        }
        return JSON;
    }

    @RequestMapping("drop.do")
    public String drop(@RequestParam String id) {
        MessageAppManagePo po = new MessageAppManagePo();
        po.setId(id);
        sysAppBpo.dropSysApp(po);
        return JSON;
    }
    
    @RequestMapping("getSysList.do")
    public String getSysList() {
          List<SysAppVo> list = sysAppBpo.getSysAppList();
          setList("sysgrid", list);
          return JSON;
    }
}
